<html> 
<head> 
    <script src="../OLLoader.js"></script> 
    <script src="../data/osm.js"></script> 
    <script type="text/javascript">
    
    function test_Format_OSM_constructor(t) { 
        t.plan(5); 
         
        var options = {'foo': 'bar'}; 
        var format = new OpenLayers.Format.OSM(options); 
        t.ok(format instanceof OpenLayers.Format.OSM, 
             "new OpenLayers.Format.OSM returns object" ); 
        t.eq(format.foo, "bar", "constructor sets options correctly"); 
        t.eq(typeof format.read, "function", "format has a read function"); 
        t.eq(typeof format.write, "function", "format has a write function"); 
        t.eq(format.externalProjection.getCode(), "EPSG:4326", 
             "default external projection is EPSG:4326"); 
    } 
    function test_Format_OSM_node(t) {
        t.plan(4);
        var f = new OpenLayers.Format.OSM();
        var features = f.read(osm_test_data['node']);
        var feat = features[0];
        t.eq(feat.attributes, {}, "attributes is empty");
        t.eq(feat.osm_id, 200545, "internal osm_id property set correctly");
        t.eq(feat.geometry.x, -1.8166417, "lon is correct"); 
        t.eq(feat.geometry.y, 52.5503033, "lat is correct"); 
    }
    function test_Format_OSM_node_with_tags(t) {
        t.plan(5);
        var f = new OpenLayers.Format.OSM();
        var features = f.read(osm_test_data['node_with_tags']);
        var feat = features[0];
        t.eq(feat.attributes, {'a':'b'}, "attributes match");
        t.eq(feat.osm_id, 200545, "internal osm_id property set correctly");
        t.eq(feat.fid, "node.200545", "OSM-based FID set correctly.");
        t.eq(feat.geometry.x, -1.8166417, "lon is correct"); 
        t.eq(feat.geometry.y, 52.5503033, "lat is correct"); 
    }
    function test_Format_OSM_way(t) {
        t.plan(8);
        var f = new OpenLayers.Format.OSM();
        var features = f.read(osm_test_data['way']);
        t.eq(features.length, 1, "One feature");
        var feat = features[0];
        t.eq(feat.osm_id, 4685537, "OSM ID set correctly.");
        t.eq(feat.fid, "way.4685537", "OSM-based FID set correctly.");
        t.eq(feat.geometry.CLASS_NAME, "OpenLayers.Geometry.Polygon", "returned as polygon");
        t.eq(feat.geometry.components[0].components.length, 11, "Correct number of components"); 
        t.eq(feat.geometry.components[0].components[0].osm_id, 29783472, "OSM ID set on components");
        t.eq(feat.geometry.toString(), "POLYGON((-1.8164007 52.5501836,-1.8170311 52.5506035,-1.8164092 52.5509559,-1.8169385 52.5513103,-1.8159626 52.5517893,-1.8145067 52.5518461,-1.8143197 52.5511883,-1.8141177 52.5506446,-1.8151451 52.5501275,-1.8157703 52.5505521,-1.8164007 52.5501836))", "WKT of feature is correct");
        t.eq(feat.attributes.landuse, "school", "landuse attribute correct");
    }
    
    function test_Format_OSM_node_way(t) {
        t.plan(5)
        var f = new OpenLayers.Format.OSM();
        var features = f.read(osm_test_data['node_way']);
        t.eq(features.length, 1, "One feature");
        var feat = features[0];
        t.eq(feat.osm_id, 21329267, "OSM ID set correctly");
        t.eq(feat.attributes.highway, "unclassified", "highway attribute is correct."); 
        t.eq(feat.geometry.CLASS_NAME, "OpenLayers.Geometry.LineString", "returned as linestring");
        t.eq(feat.geometry.components.length, 12, "correct number of segments");
    }
    
    function test_Format_OSM_node_way_checkTags(t) {
        t.plan(9)
        var f = new OpenLayers.Format.OSM({'checkTags': true});
        var features = f.read(osm_test_data['node_way']);
        t.eq(features.length, 3, "multiple features");
        
        var feat = features[1];
        t.eq(feat.geometry.CLASS_NAME, "OpenLayers.Geometry.Point", "point class");
        t.ok(feat.attributes != {}, "feature has attributes");
        
        var feat = features[2];
        t.eq(feat.geometry.CLASS_NAME, "OpenLayers.Geometry.Point", "point class");
        t.ok(feat.attributes != {}, "feature has attributes");
        
        feat = features[0];
        t.eq(feat.osm_id, 21329267, "OSM ID set correctly");
        t.eq(feat.attributes.highway, "unclassified", "highway attribute is correct."); 
        t.eq(feat.geometry.CLASS_NAME, "OpenLayers.Geometry.LineString", "returned as linestring");
        t.eq(feat.geometry.components.length, 12, "correct number of segments");
    }

    function test_Format_OSM_serialize(t) {
        t.plan(4);
        var f = new OpenLayers.Format.OSM({'checkTags': true});
        for (var key in osm_serialized_data) {
            var input = f.read(osm_test_data[key]);
            var output = f.write(input);
            output = output.replace(/<\?[^>]*\?>/, '');
            t.eq(output, osm_serialized_data[key], key + " serialized correctly");
        }
    }   
    function test_Format_OSM_write_reproject(t) {
        t.plan(1);
        var f = new OpenLayers.Format.OSM({'internalProjection': new OpenLayers.Projection("EPSG:900913")});
        var feat = new OpenLayers.Feature.Vector(
            new OpenLayers.Geometry.Point(100000, 100000)
            );
        var data = f.write([feat]);
        var f = new OpenLayers.Format.OSM();
        var features = f.read(data);

        t.eq(OpenLayers.Util.toFloat(features[0].geometry.x, 3), .898, "exported to lonlat and re-read as lonlat correctly")
    }    
    </script> 
</head> 
<body> 
</body> 
</html> 
